package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.MenuGoods;
import com.ruoyi.system.mapper.FileSystemMapper;
import com.ruoyi.system.mapper.MenuGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.service.IGoodsService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-04
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private FileSystemMapper fileSystemMapper;

    @Autowired
    private MenuGoodsMapper menuGoodsMapper;

    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Long id)
    {
        Goods goods = goodsMapper.selectGoodsById(id);
        if (goods.getPic()!=null){
            goods.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(goods.getPic())));
        }

        return goods;
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        List<Goods> goodslist = goodsMapper.selectGoodsList(goods);
        for (Goods good : goodslist) {
            if (good.getPic()!=null){
                good.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(good.getPic())));
            }
            List<MenuGoods> menuGoods = menuGoodsMapper.selectMenuGoodsByGoodsId(good.getId());
            if (menuGoods != null && !menuGoods.isEmpty()) {
                StringBuilder menuIds = new StringBuilder();
                for (MenuGoods menuGood : menuGoods) {
                    if (menuGood != null && menuGood.getMenuId() != null) {
                        menuIds.append(menuGood.getMenuId()).append(",");
                    }
                }
                // 移除末尾多余的逗号
                if (menuIds.length() > 0) {
                    menuIds.setLength(menuIds.length() - 1);
                }
                good.setMenuGoods(menuIds.toString());
            }
        }
        return goodslist;
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {
        goods.setCreateTime(DateUtils.getNowDate());
        if (goods.getPicFile()!=null){
            goods.setPic(goods.getPicFile().getId().toString());
        }
        int i = goodsMapper.insertGoods(goods);
        if (i>0){
            String[] split = goods.getMenuGoods().split(",");
            for (String menuId : split) {
                MenuGoods menuGoods = new MenuGoods();
                menuGoods.setMenuId(Long.valueOf(menuId));
                menuGoods.setGoodsId(goods.getId());
                menuGoodsMapper.insertMenuGoods(menuGoods);
            }
        }
        return i;
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        goods.setUpdateTime(DateUtils.getNowDate());
        if (goods.getPicFile()!=null){
            goods.setPic(goods.getPicFile().getId().toString());
        }
        int i = goodsMapper.updateGoods(goods);
        if (i>0){
            String[] split = goods.getMenuGoods().split(",");
            menuGoodsMapper.deleteMenuGoodsByMenuId(goods.getId());
            for (String menuId : split) {
                MenuGoods menuGoods = new MenuGoods();
                menuGoods.setMenuId(Long.valueOf(menuId));
                menuGoods.setGoodsId(goods.getId());
                menuGoodsMapper.insertMenuGoods(menuGoods);
            }
        }
        return i;
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        return goodsMapper.deleteGoodsById(id);
    }
}
